package xgggh.activity;

import android.arch.lifecycle.ViewModelProvider;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.f2prateek.rx.preferences2.RxSharedPreferences;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import javax.inject.Inject;
import rrrmai.apps.App;
import rrrmai.di.component.ActivityComponent;
import rrrmai.di.component.AndroidComponent;
import rrrmai.di.component.ApiComponent;
import rrrmai.di.component.ApplicationComponent;
import rrrmai.di.component.DaggerActivityComponent;
import rrrmai.di.component.DaggerAndroidComponent;
import rrrmai.di.component.DaggerApiComponent;
import rrrmai.di.component.DaggerViewModelComponent;
import rrrmai.di.component.ViewModelComponent;
import rrrmai.di.module.ActivityModule;
import rrrmai.di.module.ApiModule;
import rrrmai.di.module.NetworkModule;
import rrrmai.di.module.RepositoryModule;
import rrrmai.room.database.AppDataBase;
import rrrmai.service.APIService;
import rrrmai.viewmodel.ContactViewModel;
import rrrmai.viewmodel.PostsViewModel;

/**
 * Created by az on 2018/3/23.
 */

public class DIActivity  extends RxAppCompatActivity {

    @Inject
    RxSharedPreferences mRxSharedPreferences;

    public RxSharedPreferences getmRxSharedPreferences() {
        return mRxSharedPreferences;
    }

    @Inject
    PostsViewModel mPostsViewModel;
    public PostsViewModel getPostViewModelProvider() {
        return mPostsViewModel;
    }

    @Inject
    ContactViewModel mContactViewModel;
    public ContactViewModel getContactViewModelProvider() {
        return mContactViewModel;
    }


    @Inject
    AppDataBase mAppDataBase;
    public AppDataBase getAppDataBase() {
        return mAppDataBase;
    }

    @Inject
    APIService mAPIService;
    public APIService getAPIService() {
        return mAPIService;
    }

    @Inject
    DIActivity mActivity;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        createAndroidDiComponent().inject(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    protected AndroidComponent createAndroidDiComponent(){
        return DaggerAndroidComponent.builder()
                .apiComponent(createAPIComponent())
                .applicationComponent(createAppComponent())
                .activityComponent(createActivityComponent())
                .viewModelComponent(createVMComponent())
                .build();
    }

    protected ViewModelComponent createVMComponent(){
        return DaggerViewModelComponent.builder()
                .repositoryModule(new RepositoryModule())
                .activityComponent(createActivityComponent())
                .apiComponent(createAPIComponent())
                .applicationComponent(createAppComponent())
                .build();
    }


    protected ActivityComponent createActivityComponent(){
        return DaggerActivityComponent.builder()
                .applicationComponent(createAppComponent())
                .activityModule(new ActivityModule(this))
                .build();
    }

    protected ApiComponent createAPIComponent(){
        ApiComponent apiComponent= DaggerApiComponent
                .builder()
                .networkModule(new NetworkModule())
                .apiModule(new ApiModule())
                .applicationComponent(createAppComponent())
                .build();
        return  apiComponent;
    }

    protected ApplicationComponent createAppComponent(){
        App app=(App) getApplication();
        return  app.getmApplicationComponent();
    }

}
